package com.ly.lypicturebackend.manager.webSocket.disruptor;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.lmax.disruptor.dsl.Disruptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * @author luyi
 * @create 2025-03-03-23:57
 */
@Configuration
public class PictureEditDisruptorConfig {
    @Resource
    @Lazy
    private PictureEditEventHandler pictureEditEventHandler;
    public static final int BUFFER_SIZE = 1024 * 256;

    @Bean("pictureEditDisruptor")
    public Disruptor<PictureEditEvent> disruptor(){
        Disruptor<PictureEditEvent> pictureEditDisruptor = new Disruptor<>(
                PictureEditEvent::new,
                BUFFER_SIZE,
                ThreadFactoryBuilder.create().setNamePrefix("pictureEditDisruptor").build()
        );
        //设置消费者
        pictureEditDisruptor.handleEventsWithWorkerPool(pictureEditEventHandler);
        pictureEditDisruptor.start();
        return pictureEditDisruptor;
    }

    @PreDestroy
    public void  destroy(){
        disruptor().shutdown();
    }
}
